import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:my_ebook_app/pages/splash.dart';
import 'package:my_ebook_app/pages/welcome.dart';
import 'package:my_ebook_app/pages/bookshelf.dart';
import 'package:my_ebook_app/pages/book-detail.dart';
import 'package:my_ebook_app/pages/home.dart';
import 'package:my_ebook_app/pages/reading.dart';
import 'package:provider/provider.dart';

import '../models/ReadingParams.dart';
import '../pages/theme_notifier.dart';

final GoRouter myRouter = GoRouter(
  routes: [
    GoRoute(
      path: '/',
      builder: (context, state) => ChangeNotifierProvider.value(
        value: Provider.of<ThemeNotifier>(context),
        child: SplashScreen(),
      ),
    ),
    GoRoute(
      path: '/welcome',
      builder: (context, state) => ChangeNotifierProvider.value(
        value: Provider.of<ThemeNotifier>(context),
        child: WelcomePage(),
      ),
    ),
    GoRoute(
      path: '/bookshelf',
      builder: (context, state) => ChangeNotifierProvider.value(
        value: Provider.of<ThemeNotifier>(context),
        child: BookshelfPage(),
      ),
    ),
    GoRoute(
      path: '/detail',
      builder: (context, state) {
        final bookName = state.extra as String;
        return ChangeNotifierProvider.value(
          value: Provider.of<ThemeNotifier>(context),
          child: BookDetailPage( bookName: bookName,),
        );
      }
    ),
    GoRoute(
      path: '/home',
      builder: (context, state) => ChangeNotifierProvider.value(
        value: Provider.of<ThemeNotifier>(context),
        child: HomePage(),
      ),
    ),
    GoRoute(
        path: '/reading',
        builder: (context, state) {
          final params = state.extra as ReadingParams;
          return ChangeNotifierProvider.value(
            value: Provider.of<ThemeNotifier>(context),
            child: ReadingPage(
                book: params.book,
                bookCatalogue: params.catalogue),
          );
      }
    ),
  ],
  observers: [
    NavigatorObserver()
  ],
);
class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();
    navigateToWelcomePage();
  }

  void navigateToWelcomePage() async {
    await Future.delayed(Duration(seconds: 2));
    if (mounted) {
      context.go('/welcome');
    }
  }

  @override
  Widget build(BuildContext context) {
    return SplashPage();
  }
}

